package cn.lili.xiaoran.util;

import cn.lili.xiaoran.entity.vo.LiQuestionnaireVo;
import com.alibaba.fastjson.JSONArray;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 问卷工具
 *
 * @author LiLang
 * @since 2022/09/21
 */
public class QuestionnaireUtil {

    private static final Map<String, Map<String, Weight>> weights = new HashMap<>();

    @Data
    @Accessors(chain = true)
    private static class Weight {
        private float weight;
        private Map<String, Float> children;
    }

    static {
        HashMap<String, Weight> manufacture = new HashMap<>();
        HashMap<String, Weight> service = new HashMap<>();
        weights.put("制造业", manufacture);
        weights.put("服务业", service);

        operateByManufacture(manufacture);
        construction(manufacture);
        outside(manufacture);
        finance(manufacture);
        manage(manufacture);

        operateByService(service);
        construction(service);
        outside(service);
        finance(service);
        manage(service);
    }

    /**
     * 外部环境
     *
     * @param item  项目
     */
    static void outside(HashMap<String, Weight> item) {
        Map<String, Float> children = new HashMap<>();
        children.put("国家政策", 0.2f);
        children.put("经济形势", 0.2f);
        children.put("科技发展水平", 0.2f);
        children.put("劳动力市场状况", 0.2f);
        children.put("产业要素获得便利性", 0.2f);
        item.put("外部环境", new Weight().setWeight(0.1f).setChildren(children));
    }

    /**
     * 组织建设
     *
     * @param item  项目
     */
    static void construction(HashMap<String, Weight> item) {
        Map<String, Float> children = new HashMap<>();
        children.put("架构体系", 0.1f);
        children.put("组织效率", 0.15f);
        children.put("文化内涵", 0.05f);
        children.put("文化认同度", 0.1f);
        children.put("人才充足率", 0.1f);
        children.put("人岗匹配", 0.1f);
        children.put("员工流失率", 0.1f);
        children.put("人均收入增长率与人工成本管控", 0.05f);
        children.put("员工培训", 0.05f);
        children.put("员工激励", 0.1f);
        children.put("员工满意度", 0.1f);
        item.put("组织建设", new Weight().setWeight(0.2f).setChildren(children));
    }

    /**
     * 科学管理
     *
     * @param item  项目
     */
    static void manage(HashMap<String, Weight> item) {
        Map<String, Float> children = new HashMap<>();
        children.put("战略管理", 0.15f);
        children.put("目标管理", 0.15f);
        children.put("计划管理", 0.15f);
        children.put("绩效管理", 0.1f);
        children.put("风险管理", 0.1f);
        children.put("诚信与合规经营", 0.1f);
        children.put("人财物独立性", 0.1f);
        children.put("智慧管理", 0.1f);
        children.put("支持与帮助", 0.05f);
        item.put("科学管理", new Weight().setWeight(0.2f).setChildren(children));
    }

    /**
     * 财税健康
     *
     * @param item  项目
     */
    static void finance(HashMap<String, Weight> item) {
        Map<String, Float> children = new HashMap<>();
        children.put("经营能力", 0.2f);
        children.put("盈利能力", 0.15f);
        children.put("成长能力", 0.15f);
        children.put("偿债能力", 0.25f);
        children.put("税务风险", 0.25f);
        item.put("财税健康", new Weight().setWeight(0.3f).setChildren(children));
    }

    /**
     * 财税健康(服务业)
     *
     * @param item  项目
     */
    static void operateByService(HashMap<String, Weight> item) {
        Map<String, Float> children = new HashMap<>();
        children.put("业务流程", 0.1f);
        children.put("分工协作", 0.1f);
        children.put("服务响应速度", 0.1f);
        children.put("信息能力", 0.05f);
        children.put("客户获得率", 0.1f);
        children.put("服务可靠性", 0.15f);
        children.put("服务补救能力", 0.05f);
        children.put("客户投诉率", 0.1f);
        children.put("客户满意度", 0.15f);
        children.put("服务创新性", 0.1f);
        item.put("高效运营", new Weight().setWeight(0.2f).setChildren(children));
    }

    /**
     * 财税健康(服务业)
     *
     * @param item  项目
     */
    static void operateByManufacture(HashMap<String, Weight> item) {
        Map<String, Float> children = new HashMap<>();
        children.put("业务流程", 0.15f);
        children.put("分工协作", 0.15f);
        children.put("流程柔性", 0.15f);
        children.put("企业的生产制造能力", 0.15f);
        children.put("产能利用率", 0.1f);
        children.put("产品市场认可度", 0.15f);
        children.put("产品创新投入", 0.15f);
        item.put("高效运营", new Weight().setWeight(0.2f).setChildren(children));
    }

    /**
     * 计算问卷积分
     *
     * @param enterpriseType    企业类型
     * @param data              问卷
     * @return                  积分
     */
    public static Map<String,Object> calcScore(String enterpriseType, List<LiQuestionnaireVo.QuestionGroup> data) {
        float score = 0.f;
        List<Map<String,Object>> weidufenshu=new ArrayList<>();
        if (data != null && data.size() > 0) {
            Map<String, Weight> ws = weights.get(enterpriseType);
            int shicount=0;
            for (LiQuestionnaireVo.QuestionGroup group : data) {
                Map<String,Object> fsmap=new HashMap<>();
                String title=group.getTitle();
                Weight gw = null;
                for (String keykk: ws.keySet()) {
                    if(keykk.contains(title) || title.contains(keykk)){
                        gw=ws.get(keykk);
                    }
                }
                float fw = gw.getWeight();
                float fs = 0.f;
                for (LiQuestionnaireVo.Question question : group.getQuestions()) {
                    String key=question.getTitle().split("：")[0];
                    Map<String, Float> map=gw.getChildren();

                    float sw =0.0f;
                    for (String kk: map.keySet()) {
                        if(key.contains(kk)){
                            sw=map.get(kk);
                        }
                    }
                    for (LiQuestionnaireVo.Answer answer : question.getAnswers()) {
                        if (answer.getChecked()) {
                            if(answer.getScore()!=null){
                                fs += (sw * answer.getScore())*20;
                                break;
                            }
                            if(answer.getIstrue()!=null){
                                if(answer.getIstrue()){
                                    shicount++;
                                }
                            }
                        }
                    }

                }

                fs *= fw;
                score += fs;
                if(title.contains("财税健康")){
                    int shuwufenxianfen=0;
                    if(shicount>7){
                        shuwufenxianfen=5;
                    }else  if(shicount==7 || shicount==6){
                        shuwufenxianfen=4;
                    }else  if(shicount==5 || shicount==4){
                        shuwufenxianfen=3;
                    }else  if(shicount==3 || shicount==2){
                        shuwufenxianfen=2;
                    }else  if(shicount==1){
                        shuwufenxianfen=1;
                    }
                    score+=shuwufenxianfen*0.075*20;
                    fs+=shuwufenxianfen*0.075*20;
                }
                fsmap.put(title,fs);
                weidufenshu.add(fsmap);
            }

        }
        Map<String,Object> reslutMap=new HashMap<>();
        reslutMap.put("score",score);
        reslutMap.put("weidufenshu", JSONArray.toJSONString(weidufenshu));
        return reslutMap;
    }

}